package com.coollang.smartbra.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.VideoView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.ActionDetailAdapter;
import com.coollang.smartbra.constant.ResourceList;
import com.coollang.smartbra.presenter.ActionDetailActivityPresenter;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: Timmy
 * @data: 2017/11/20
 * @Fuction: 动作库运动详情
 */
public class ActionDetailActivity extends MyAutoLayoutActivity {
    @BindView(R.id.status_bar)
    View mStatusBar;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.content)
    LinearLayout mContent;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.radio)
    RadioGroup mRadioGroup;
    @BindView(R.id.rb_left)
    RadioButton mRbLeft;
    @BindView(R.id.rb_right)
    RadioButton mRbRight;
    @BindView(R.id.recycleview)
    RecyclerView mRecycleView;
    @BindView(R.id.videoview)
    VideoView mVideoView;

    public int mCurrentPosition;

    public ActionDetailAdapter mDetailAdapter;
    public ActionDetailActivityPresenter mPresenter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_action_detail);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(mStatusBar, this, mContent);

        processIntent();
        initViews();
    }

    @Override
    protected void onStart() {
        super.onStart();
        startVideo();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopVideo();
    }


    private void initViews() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
                overridePendingTransition(R.anim.close_enter, R.anim.close_exit);

            }
        });
        mIvBack.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.back_bottom));
        mTvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_80));

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_left:
                        // 点击左边详细说明
                        clickDetailedDescription();
                        break;
                    case R.id.rb_right:
                        // 点击右边常见错误
                        clickCommonMistakes();
                        break;
                    default:
                }
            }
        });


        mDetailAdapter = new ActionDetailAdapter(null);
        // 取消RecycleView的动画
        mDetailAdapter.isFirstOnly(false);

        mRecycleView.setItemAnimator(new DefaultItemAnimator());
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mRecycleView.setAdapter(mDetailAdapter);
    }

    @Override
    protected void initData() {
        clickDetailedDescription();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            // 处理点击后退键返回的情况
            this.finish();
            //finish当前activity
            overridePendingTransition(R.anim.close_enter, R.anim.close_exit);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 点击左边详细说明
     */
    private void clickDetailedDescription() {
        mRbLeft.setBackgroundResource(R.drawable.action_detail_left);
        mRbRight.setBackgroundResource(R.color.tra);
        mPresenter.loadLeftData(mCurrentPosition);
    }

    /**
     * 点击右边常见错误
     */
    private void clickCommonMistakes() {
        mRbRight.setBackgroundResource(R.drawable.action_detail_right);
        mRbLeft.setBackgroundResource(R.color.tra);
        mPresenter.loadRightData(mCurrentPosition);
    }

    /**
     * 获取上一个页面传递过来的数据
     */
    private void processIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mCurrentPosition = getIntent().getIntExtra("position", 0);
            String[] actionName = ResourceList.ACTION_NAME_LIST;
            mTvTitle.setText(actionName[mCurrentPosition]);
            mPresenter = new ActionDetailActivityPresenter(this);
        }
    }

    private String getMedid(int num) {
        return "android.resource://" + this.getPackageName() + "/" + ResourceList.ACTION_LIST_VIDEOS[num];
    }

    /**
     * 开始播放视频
     */
    private void startVideo() {
        if (mVideoView != null && !mVideoView.isPlaying()) {
            mVideoView.setVideoPath(getMedid(mCurrentPosition));
            mVideoView.start();
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    startVideo();
                }
            });
        }
    }

    /**
     * 暂停播放视频
     */
    public void stopVideo() {
        if (mVideoView != null) {
            if (mVideoView.isPlaying()) {
                mVideoView.stopPlayback();
            }
        }
    }
}
